/*
** FUNCIONES FUSE 
*/

//#include "StPfs.h"

static const char *pfs_str = "ESTO ES PFS!\n";
static const char *pfs_path = "/pfs";

static int pfs_getattr(const char *path, struct stat *stbuf)
{
    int res = 0;
	
	printf("Entro al GETATTR\n");
    memset(stbuf, 0, sizeof(struct stat));
    if(strcmp(path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
    }
    else if(strcmp(path, pfs_path) == 0) {
        stbuf->st_mode = S_IFREG | 0444;
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(pfs_str);
	}
	else
        res = -ENOENT;	  
	return res;
}
  
static int pfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                           off_t offset, struct fuse_file_info *fi)
{
	(void) offset;
	(void) fi;
  
	printf("Entro al READDIR\n");
	if(strcmp(path, "/") != 0)
		return -ENOENT;
  
	filler(buf, ".", NULL, 0);
	filler(buf, "..", NULL, 0);
	filler(buf, pfs_path + 1, NULL, 0);
  
	return 0;
}

static int pfs_open(const char *path, struct fuse_file_info *fi)
{
    if(strcmp(path, pfs_path) != 0)
        return -ENOENT;
  
    if((fi->flags & 3) != O_RDONLY)
        return -EACCES;
  
    return 0;
}
  
static int pfs_read(const char *path, char *buf, size_t size, off_t offset,
                        struct fuse_file_info *fi)
{
    size_t len;
    (void) fi;
    if(strcmp(path, pfs_path) != 0)
        return -ENOENT;
  
    len = strlen(pfs_str);
    if (offset < len) {
        if (offset + size > len)
		size = len - offset;
        memcpy(buf, pfs_str + offset, size);
    } else
        size = 0;
  
    return size;
}